import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const types = {
  SET_IS_AUTHENTICATION: 'SET_IS_AUTHENTICATION', // 是否认证通过
  SET_USER: 'SET_USER' // 用户信息
}

const state = {
  isAuthentication: false, // 是否认证
  user: {} // 存储用户信息
}

const getters = {
  isAuthentication: state => state.isAuthentication,
  user: state => state.user
}

const mutations = {
  [types.SET_IS_AUTHENTICATION](state, isAuthentication) {
    if (isAuthentication) {
      state.isAuthentication = isAuthentication
    } else {
      state.isAuthentication = false
    }
  },
  [types.SET_USER](state, user) {
    if (user) {
      state.user = user
    } else {
      state.user = {}
    }
  }
}

const actions = {
  // 设置登录状态
  setIsAuthentication: ({ commit }, isAuthentication) => {
    commit(types.SET_IS_AUTHENTICATION, isAuthentication)
  },
  // 设置user
  setUser: ({ commit }, user) => {
    commit(types.SET_USER, user)
  },
  // 退出，清空当前登录用户信息
  clearCurrentState: ({ commit }) => {
    commit(types.SET_IS_AUTHENTICATION, false)
    commit(types.SET_USER, null)
  }
}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})
